package com.isoftstone.merchant.service.mdept.impl;

import com.isoftstone.common.Page;
import com.isoftstone.merchant.dao.mdept.MDeptMapper;
import com.isoftstone.merchant.entity.mdept.MDept;
import com.isoftstone.merchant.service.mdept.IMdeptService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by as on 2017/11/28.
 */
@Service
public class MdeptService implements IMdeptService {

    @Autowired
    MDeptMapper mDeptMapper;

    public List<MDept> getDeptlistPage(Page page) throws Exception {
        return mDeptMapper.getDeptlistPage(page);
    }

    @Override
    public List<MDept> getDeptAll(@Param("corpId") String corpId) throws Exception {
        return mDeptMapper.getDeptAll1(corpId);
    }

    /**
     * 递归向下查询
     *
     * @param corpId
     * @return
     * @throws Exception
     */
    @Override
    public List<MDept> getDownDeptAll(String corpId) throws Exception {
        List<MDept> list = mDeptMapper.getDeptAll(corpId);
        for (MDept dept : list) {
            List<MDept> mDept = getDownDeptlistdigui(dept.getDeptId());
            dept.setmDept(mDept);
        }
        return list;
    }

    public List<MDept> getDownDeptlistdigui(String deptId) throws Exception {
        List<MDept> list = mDeptMapper.getDeptlistBySuperId(deptId);
        for (MDept dept : list) {
            List<MDept> superId = this.getDownDeptlistdigui(dept.getDeptId());
            dept.setmDept(superId);
        }
        return list;
    }

    /**
     * 递归向上查询
     * @param deptId
     * @return
     * @throws Exception
     */
    @Override
    public List<MDept> getUpDeptAll(String deptId) throws Exception {
        List<MDept> list = mDeptMapper.getUpDeptAll(deptId);
        for (MDept dept : list) {
            List<MDept> mDept = getUpDeptlistdigui(dept.getSuperDeptId());
            dept.setmDept(mDept);
        }
        return list;
    }

    public List<MDept> getUpDeptlistdigui(String superId) throws Exception {
        List<MDept> mDeptList = mDeptMapper.queryDeptBySuperId(superId);
        for (MDept mDept : mDeptList) {
            List<MDept> deptList = this.getUpDeptlistdigui(mDept.getSuperDeptId());
            mDept.setmDept(deptList);
        }
        return mDeptList;
    }

    @Override
    public MDept getDeptById(String DId) throws Exception {
        return mDeptMapper.selectByPrimaryKey(DId);
    }

    @Override
    public MDept getRootDept(String corpId) throws Exception {
        return mDeptMapper.getRootDept(corpId);
    }

    @Transactional
    public void saveDept(MDept dept) throws Exception {
        mDeptMapper.addDept(dept);
    }

    @Override
    public String selectdept(String deptName, String superDeptId,String corpId) throws Exception {
        return mDeptMapper.selectdept(deptName, superDeptId,corpId);
    }

    @Transactional
    public void deleteDept(String deptId) throws Exception {
        mDeptMapper.deleteByPrimaryKey(deptId);
    }

    @Override
    public MDept getparaentDept(String deptId) throws Exception {
        return mDeptMapper.getparaentDept(deptId);
    }

    @Transactional
    public void updateDept(MDept dept) throws Exception {
        mDeptMapper.updateByPrimaryKeySelective(dept);
    }

    @Override
    public List<MDept> tongjiDepts(String deptId) throws Exception {
        return mDeptMapper.tongjiDepts(deptId);
    }

    @Override
    public MDept getMDeptBymId(String corpId) throws Exception {
        return mDeptMapper.getMDeptBymId(corpId);
    }

    @Override
    public List<MDept> getDeptAllTree() {
        return mDeptMapper.getDeptAllTree();
    }

    @Override
    public void insertFirstdept(MDept dept) throws Exception {
        mDeptMapper.insertFirstdept(dept);
    }

    //缴费用户使用
    @Override
    public MDept getDeptbyIdFeeUser(String deptId, String superDeptId) throws Exception {
        return mDeptMapper.selectDeptByIdFeeUser(deptId, superDeptId);
    }


    @Override
    public List<MDept> getAllDeptByCorpId(String corpId) throws Exception {
        return mDeptMapper.getAllDeptByCorpId(corpId);
    }

    @Override
    public  List<MDept> getDeptByNameAndLevel(Map<String, Object> map) throws Exception {
        return mDeptMapper.selectDeptByNameAndLevel(map);
    }

    @Override
    public String getUserDeptById(String deptId) throws Exception {
        return mDeptMapper.getUserDeptById(deptId);
    }

    @Override
    public List<MDept> getParentDeptByIdandCorpId(String deptId, String corpId) throws Exception {
        return mDeptMapper.getParentDeptByIdandCorpId(deptId, corpId);
    }

    @Override
    public MDept getDeptBySuperId(@Param("superId") String superId) throws Exception {
        return mDeptMapper.getDeptBySuperId(superId);
    }

    @Override
    public List<MDept> queryDeptBySuperId(@Param("superId") String superId) throws Exception {
        return mDeptMapper.queryDeptBySuperId(superId);
    }


    /**
     * 商户端缴费机构递归向下传查询
     */
    /*
    List<MDept> departList = null;

    @Override
    public List<MDept> getDowndetptree(String deptId) {
        try {
            MDept mDept = mDeptMapper.getTestDeptAll(deptId);
            if (null != mDept) {
                departList.add(mDept);
            }
            if (null != mDept) {
                getDowndetptree(mDept.getDeptId());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return departList;
    }

    @Override
    public List<MDept> getDownALLtree(String deptId) throws Exception {

        departList = new ArrayList<>();

        MDept mDept = mDeptMapper.getdeptById(deptId);

        if (mDept != null) {
            departList.add(mDept);
            getDowndetptree(deptId);
        }
        return departList;
    }

    *//**
     * 新增缴费用户机构向上查询
     *
     * @param
     * @return
     * @throws Exception
     */
    List<MDept> superDeptList = null;

    @Override
    public List<MDept> getUpdetptree(String superId) {
        try {
            MDept mDept = mDeptMapper.getDeptBySuperId(superId);
            if (null != mDept && mDept.getSuperDeptId() != "0") {
                superDeptList.add(mDept);
            }
            if (null != mDept && mDept.getSuperDeptId() != "0") {
                getUpdetptree(mDept.getSuperDeptId());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return superDeptList;
    }

    @Override
    public List<MDept> getUpAlltree(String deptId) {

        superDeptList = new ArrayList<>();
        try {
            MDept mDept = mDeptMapper.getdeptById(deptId);
            if (mDept != null) {
                if (!StringUtils.equals(mDept.getSuperDeptId(), "0")) {
                    superDeptList.add(mDept);
                    getUpdetptree(mDept.getSuperDeptId());
                }else{
                    superDeptList.add(mDept);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return superDeptList;
    }


    @Override
    public String getDeptNameBydeptId(String deptId) throws Exception {
        return mDeptMapper.getDeptNameBydeptId(deptId);
    }

    @Override
    public Integer getleavlBydeptIdandCorpId(String deptId, String corpId) throws Exception {
        return mDeptMapper.getleavlBydeptIdandCorpId(deptId,corpId);
    }

    @Override
    public String getdeptIdBydeptId(String deptId) throws Exception {
        return mDeptMapper.getdeptIdBydeptId(deptId);
    }

    @Override
    public List<MDept> getDeptBySupId(String superDeptId) throws Exception {
        return mDeptMapper.getDeptBySupId(superDeptId);
    }

    @Override
    public List<MDept> getsonDept(String superDeptId) throws Exception {
        return mDeptMapper.getTestDeptAll(superDeptId);
    }

    @Override
    public void saveExcelData(List<MDept> resultList) throws Exception {
        if(resultList!=null){
            for (MDept mDept : resultList) {
                if (mDept!=null){
                    mDeptMapper.addDept(mDept);
                }
            }
        }
    }


}
